package com.zyy.smartcity1129.reservation;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.MainActivity;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.CarConsumptionInfo;
import com.zyy.smartcity1129.data.MyCarInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.data.RvReserNowInfo;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: ReserNowFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 29/11/2021$
 * @Version: 1.0
 */
public class ReserNowFragment extends BaseFragment {
    private Button btnReserNowAdd;
    private RecyclerView rvReserNow;
    UserInfo.UserBean userBean;
    List<MyCarInfo.RowsBean> myCarList = new ArrayList<>();
    List<CarConsumptionInfo.RowsBean> carConList = new ArrayList<>();
    List<RvReserNowInfo> reserNowInfoList = new ArrayList<>();
    RvAd<RvReserNowInfo> rvAd;
    private FrameLayout frameP1;
    private EditText editPPlateNo;
    private EditText editPType;
    private EditText editPTravelDistance;
    private Button button4;
    private FrameLayout frameP2;
    private int id;

    @Override
    protected int layId() {
        return R.layout.f_reser_now;
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        ReservationFragment.mutableLiveData.postValue("立即预约");
        initView(iv);
        XCUtli xcUtli = new XCUtli(3);
        xcUtli.then(() -> {
            myCarList = Http.getInstance().get("/prod-api/api/park/car/my", null, MyCarInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            userBean = Http.getInstance().get("/prod-api/api/common/user/getInfo", null, UserInfo.class, 1).getUser();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            carConList = Http.getInstance().get("/prod-api/api/park/car/consumption", null, CarConsumptionInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), atomicInteger -> {
            if (atomicInteger.get() == 3) {
                initDataList();
                initAdapter(reserNowInfoList);
                rvReserNow.setLayoutManager(new LinearLayoutManager(getContext()));
                rvReserNow.setAdapter(rvAd);
            }
        });
        btnReserNowAdd.setOnClickListener(view -> {
            initPPUtil(0, 0, "");
        });
    }

    private void initDataList() {
        for (int i = 0; i < myCarList.size(); i++) {
            String travelDistance = "暂无里程数";
            for (int y = 0; y < carConList.size(); y++) {
                if (myCarList.get(i).getPlateNo().equals(carConList.get(y).getPlateNo())) {
                    travelDistance = String.valueOf(carConList.get(y).getTravelDistance());
                }
            }
            reserNowInfoList.add(new RvReserNowInfo(myCarList.get(i).getId(), myCarList.get(i).getPlateNo(), myCarList.get(i).getType(), travelDistance, userBean.getPhonenumber()));
        }
    }

    private void initAdapter(List<RvReserNowInfo> reserNowInfoList) {
        rvAd = new RvAd<RvReserNowInfo>(reserNowInfoList, R.layout.r_reser_now) {
            private void initView(View vi) {
                tvRvPlateNo = (TextView) vi.findViewById(R.id.tvRvPlateNo);
                tvRvType = (TextView) vi.findViewById(R.id.tvRvType);
                tvRvTravelDistance = (TextView) vi.findViewById(R.id.tvRvTravelDistance);
                tvRvPhonenumber = (TextView) vi.findViewById(R.id.tvRvPhonenumber);
                imgRvReserNowDel = (ImageView) vi.findViewById(R.id.imgRvReserNowDel);
                lineRvReserNow = (LinearLayout) vi.findViewById(R.id.lineRvReserNow);
            }

            private TextView tvRvPhonenumber;
            private TextView tvRvTravelDistance;
            private TextView tvRvType;
            private TextView tvRvPlateNo;
            private ImageView imgRvReserNowDel;
            private LinearLayout lineRvReserNow;

            @Override
            protected void initR(View vi, RvReserNowInfo rvReserNowInfo, int p) {
                initView(vi);
                tvRvPlateNo.setText("车牌号：" + rvReserNowInfo.getPlateNo());
                tvRvType.setText("车辆类型：" + rvReserNowInfo.getType());
                tvRvTravelDistance.setText("公里数：" + rvReserNowInfo.getTravelDistance());
                tvRvPhonenumber.setText("手机号：" + rvReserNowInfo.getPhonenumber());
                id = rvReserNowInfo.getId();
                lineRvReserNow.setOnClickListener(view -> {
                    initPPUtil(1, id, rvReserNowInfo.getPlateNo());
                });
                imgRvReserNowDel.setOnClickListener(view -> {
                    th(new Runnable() {
                        @Override
                        public void run() {
                            JsonObject jsonObject = new JsonObject();
                            String msg1 = Http.getInstance().get("/prod-api/api/park/car/" + id, RequestBody.create(Http.mm, jsonObject.toString()), ResultInfo.class, 4).getMsg();
                            JsonObject jsonObject2 = new JsonObject();
                            String msg2 = Http.getInstance().get("/prod-api/api/park/car/consumption/" + id, RequestBody.create(Http.mm, jsonObject2.toString()), ResultInfo.class, 4).getMsg();
                            initNotify();
                            ui(new Runnable() {
                                @Override
                                public void run() {
                                    Log.i("TAG", String.valueOf(msg1));
                                    Log.i("TAG", String.valueOf(msg2));
                                    Toast.makeText(getContext(), msg1, Toast.LENGTH_SHORT).show();
                                    Toast.makeText(getContext(), msg2, Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                });
            }
        };
    }

    private void initNotify() {
        th(new Runnable() {
            @Override
            public void run() {
                myCarList = Http.getInstance().get("/prod-api/api/park/car/my", null, MyCarInfo.class, 1).getRows();
                carConList = Http.getInstance().get("/prod-api/api/park/car/consumption", null, CarConsumptionInfo.class, 1).getRows();
                reserNowInfoList.clear();
                initDataList();
                ui(new Runnable() {
                    @Override
                    public void run() {
                        initAdapter(reserNowInfoList);
                        rvReserNow.setAdapter(rvAd);
                    }
                });
            }
        });
    }

    private void initPPUtil(int b, int id, String mPlateNo) {
        PpUtil ppUtil = new PpUtil();
        View v = ppUtil.getP(requireContext(), R.layout.p_reser_now);
        TextView tvPPlateNo = v.findViewById(R.id.tvPPlateNo);
        frameP1 = (FrameLayout) v.findViewById(R.id.frame_p1);
        editPPlateNo = (EditText) v.findViewById(R.id.editPPlateNo);
        editPType = (EditText) v.findViewById(R.id.editPType);
        editPTravelDistance = (EditText) v.findViewById(R.id.editPTravelDistance);
        button4 = (Button) v.findViewById(R.id.button4);
        if (b == 0) {
            button4.setText("添加");
        }else if (b == 1) {
            button4.setText("修改");
        }
        frameP2 = (FrameLayout) v.findViewById(R.id.frame_p2);
        if (b != 0) {
            tvPPlateNo.setVisibility(View.VISIBLE);
            editPPlateNo.setVisibility(View.GONE);
            tvPPlateNo.setText(mPlateNo);
        } else {
            tvPPlateNo.setVisibility(View.GONE);
            editPPlateNo.setVisibility(View.VISIBLE);
        }
        frameP1.setOnClickListener(view -> {
            ppUtil.closeP();
        });
        frameP2.setOnClickListener(view -> {
            ppUtil.closeP();
        });
        button4.setOnClickListener(view -> {
            String plateNo;
            if (b != 0) {
                plateNo = tvPPlateNo.getText().toString().trim();
            } else {
                plateNo = editPPlateNo.getText().toString().trim();
            }
            String type = editPType.getText().toString().trim();
            double travelDistance = 0;
            if (!editPTravelDistance.getText().toString().trim().equals("")) {
                travelDistance = Double.parseDouble(editPTravelDistance.getText().toString().trim());
            }
            Log.i("TAG", String.valueOf(travelDistance));
            if (!plateNo.equals("") && !type.equals("") && travelDistance!=0) {
                double finalTravelDistance = travelDistance;
                Log.i("TAG", String.valueOf(finalTravelDistance));
                th(new Runnable() {
                    @Override
                    public void run() {
                        String msg1 = " ";
                        String msg2 = " ";
                        if (b == 0) {
                            JsonObject jsonObject = new JsonObject();
                            jsonObject.addProperty("plateNo", plateNo);
                            jsonObject.addProperty("type", type);
                            msg1 = Http.getInstance().get("/prod-api/api/park/car", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                            JsonObject jsonObject2 = new JsonObject();
                            jsonObject2.addProperty("plateNo", plateNo);
                            jsonObject2.addProperty("travelDistance", finalTravelDistance);
                            msg2 = Http.getInstance().get("/prod-api/api/park/car/consumption", RequestBody.create(Http.m, jsonObject2.toString()), ResultInfo.class, 2).getMsg();
                        }  else if (b == 1){
                            JsonObject jsonObject = new JsonObject();
                            jsonObject.addProperty("id", id);
                            jsonObject.addProperty("plateNo", plateNo);
                            jsonObject.addProperty("type", type);
                            msg1 = Http.getInstance().get("/prod-api/api/park/car", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                            JsonObject jsonObject2 = new JsonObject();
                            jsonObject2.addProperty("id", id);
                            jsonObject2.addProperty("plateNo", plateNo);
                            jsonObject2.addProperty("travelDistance", finalTravelDistance);
                            msg2 = Http.getInstance().get("/prod-api/api/park/car/consumption", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                        }
                        String finalMsg = msg1;
                        String finalMsg1 = msg2;
                        ui(new Runnable() {
                            @Override
                            public void run() {
                                Log.i("TAG", String.valueOf(finalMsg));
                                Log.i("TAG", String.valueOf(finalMsg1));
                                Toast.makeText(getContext(), finalMsg, Toast.LENGTH_SHORT).show();
                                Toast.makeText(getContext(), finalMsg1, Toast.LENGTH_SHORT).show();
                            }
                        });
                        initNotify();
                    }
                });
                ppUtil.closeP();
            } else {
                Toast.makeText(getContext(), "请完善信息", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView(View iv) {
        btnReserNowAdd = (Button) iv.findViewById(R.id.btnReserNowAdd);
        rvReserNow = (RecyclerView) iv.findViewById(R.id.rvReserNow);
    }
}